package com.wanmait.gohome.controller.manage;

import com.github.pagehelper.PageInfo;
import com.wanmait.gohome.pojo.Admin;
import com.wanmait.gohome.pojo.Userinfo;
import com.wanmait.gohome.service.AdminService;
import com.wanmait.gohome.service.LogSheetService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("/manage")
public class ManageController {
 @Resource
 private AdminService adminService;
 @Resource
 private LogSheetService logSheetService;
@GetMapping("/admin/insert")
 public void insert(){}

//添加管理员
 @PostMapping("/admin/insert")
 public void insert2(Admin admin,String aa)
 {
   admin.setRegTime(new Date());
  admin.setAccount(aa);
  adminService.insert(admin);

 }
 @GetMapping("/admin/updatePass")
 public void updatePass(){}
 //通过id找到密码
 @PostMapping("/admin/findPass")
 @ResponseBody
 public String findPass(String passWord,Integer id) {
  Admin pass = adminService.findOldPass(passWord, id);
  if(pass==null){
   return "false";
  }
  else {
   return "true";
  }
 }
 //修改密码
 @PostMapping("/admin/updatePass")
 public String updatePass(String password,Integer id){
 adminService.updatePass(password,id);
 return "redirect:/manage/login";
 }
 //管理员显示界面
 @GetMapping("/admin/show")
 public void show(){}

 @PostMapping("ajaxJsp/adminShow")
 @ResponseBody
 public ModelAndView show2(){
  PageInfo<Admin> adminPageInfo = adminService.findPage();
  ModelAndView mv = new ModelAndView();
  mv.addObject("adminPageInfo",adminPageInfo);
  mv.setViewName("manage/ajaxJsp/adminShow");
    return mv;
 }
 //单个删除管理员
    @PostMapping("/admin/delete")
    @ResponseBody
    public  void delete(Integer id){
    adminService.update(id);
//     System.out.println(id);
    }
    @RequestMapping("/admin/findLogs")
     public void findLogs()
    {}
    @RequestMapping("/ajaxJsp/findLog")
    @ResponseBody
    public ModelAndView findLog()
    {
      ModelAndView mv = new ModelAndView();
     PageInfo pageInfo   = logSheetService.findPage();
    mv.addObject("pageInfo",pageInfo);
     return mv;
}
//封禁
@PostMapping("/admin/banned")
 @ResponseBody
 public void banned(Integer id){
adminService.banned(id);
}
//解封
    @PostMapping("/admin/deblocking")
    @ResponseBody
    public void deblocking(Integer id){
    adminService.deblocking(id);
    }

    @PostMapping("Reason")
    @ResponseBody()
    public void insertReason(Admin admin){
        System.out.println(admin);
        adminService.insertReject(admin);
    }
}
